package org.spiderflow.oss.executor.shape;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.spiderflow.context.SpiderContext;
import org.spiderflow.executor.ShapeExecutor;
import org.spiderflow.model.Shape;
import org.spiderflow.model.SpiderNode;
import org.spiderflow.oss.model.Oss;
import org.springframework.stereotype.Component;

@Component
public class OssExecutor implements ShapeExecutor{
	
	public static final String ENDPOINT = "endpoint";
	
	public static final String ACCESS_KEY_ID = "accessKeyId";
	
	public static final String ACCESS_KEY_SECRET = "accessKeySecret";
	
	public static final String BUCKET_NAME = "bucketName";
	
	public static final String OSS_CONTEXT_KEY = "$oss_";
	
	@Override
	public String supportShape() {
		return "oss";
	}
	
	@Override
	public Shape shape() {
		Shape shape = new Shape();
		shape.setImage("");
		shape.setLabel("OSS");
		shape.setName("oss");
		shape.setTitle("OSS");
		return shape;
	}

	@Override
	public void execute(SpiderNode node, SpiderContext context, Map<String, Object> variables) {
		String endpoint = node.getStringJsonValue(ENDPOINT);
		String accessKeyId = node.getStringJsonValue(ACCESS_KEY_ID);
		String accessKeySecret = node.getStringJsonValue(ACCESS_KEY_SECRET);
		String bucketName = node.getStringJsonValue(BUCKET_NAME);
		if (StringUtils.isBlank(endpoint)) {
			context.debug("endpoint不能为空！");
		} else if (StringUtils.isBlank(accessKeyId)) {
			context.debug("accessKeyId不能为空！");
		} else if (StringUtils.isBlank(accessKeySecret)) {
			context.debug("accessKeySecret不能为空！");
		} else if (StringUtils.isBlank(bucketName)) {
			context.debug("bucketName不能为空！");
		} else {
			Oss oss = new Oss(endpoint,accessKeyId,accessKeySecret,bucketName);
			context.put(OSS_CONTEXT_KEY + node.getNodeId(), oss);
		}
	}

}
